/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.gestion;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.gestion.exception.ArticulosConsumidosException;
import co.gov.medellin.sdep.sisdep.gestion.exception.ArticulosConsumidosRuntimeException;
import co.gov.medellin.sdep.sisdep.gestion.service.IArticulosConsumidosService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.model.ArticulosConsumidos;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;

/**
 * @author Usuario
 * 
 */
public class ArticulosConsumidosController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public ArticulosConsumidosController() {
		this.articulosConsumidosListModel = new ListDataModel();
	}

	/** bean para el manejo del formulario de busqueda ArticulosConsumidos */
	private ArticulosConsumidos articulosConsumidosSearch = new ArticulosConsumidos();

	/** Objeto contenedor del listado de ArticulosConsumidos luego de una consulta. */
	public transient DataModel articulosConsumidosListModel;

	/** bean para el manejo del formulario de creacion/edicion. ArticulosConsumidos */
	public ArticulosConsumidos articulosConsumidos = new ArticulosConsumidos();

	/** instancia de servicio articulosConsumidosService */
	public IArticulosConsumidosService articulosConsumidosService;

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------
	/**
	 * 
	 * @return String
	 */
	public String searchArticulosConsumidosByKey( IArticulosConsumidosService articulosConsumidosService) {
		String msg = "";
		try {
			List valores = null;
			valores =  articulosConsumidosService.load(1, 10,
					this.articulosConsumidosSearch);
			this.articulosConsumidosListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.articulosConsumidosListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.articulosConsumidosListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.ARTICULOS_CONSUMIDOS_SEARCH_VIEW;
	}
	/**
	 * 
	 * @return String
	 */
	public String searchArticulosConsumidosByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getArticulosConsumidosService().load(1, 10,
					this.articulosConsumidosSearch);
			this.articulosConsumidosListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.articulosConsumidosListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.articulosConsumidosListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.ARTICULOS_CONSUMIDOS_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.articulosConsumidos.getId().longValue() == 0L) {
				getArticulosConsumidosService().insert(this.articulosConsumidos);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (ArticulosConsumidosException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ArticulosConsumidosRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- articulosConsumidos " + msg, null, null);
		return AppConstants.ARTICULOS_CONSUMIDOS_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String create(ArticulosConsumidos articulos,IArticulosConsumidosService service) {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (articulos.getId().longValue() == 0L) {
				service.insert(articulos);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (ArticulosConsumidosException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ArticulosConsumidosRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- articulosConsumidos " + msg, null, null);
		return AppConstants.ARTICULOS_CONSUMIDOS_SEARCH_VIEW;
	}
	
	/**
	 * 
	 * @return
	 */
	public String update(ArticulosConsumidos articulos,IArticulosConsumidosService service) {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (articulos.getId().longValue() != 0L) {
				service.update(articulos);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (ArticulosConsumidosException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ArticulosConsumidosRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.ARTICULOS_CONSUMIDOS_SEARCH_VIEW;
	}
	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.articulosConsumidos.getId().longValue() != 0L) {
				getArticulosConsumidosService().update(this.articulosConsumidos);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (ArticulosConsumidosException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ArticulosConsumidosRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.ARTICULOS_CONSUMIDOS_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createArticulosConsumidos() {
		articulosConsumidos = new ArticulosConsumidos();
		articulosConsumidos.setId(new Long(0L));
		//articulosConsumidos.setCodigo("");
		//articulosConsumidos.setValor("");
		//articulosConsumidos.setActivo("N");
		return AppConstants.ARTICULOS_CONSUMIDOS_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateArticulosConsumidos() {
		Map params = getExternalContext().getRequestParameterMap();
		String articulosConsumidosDataIndex = (String) params
				.get("articulosConsumidosDataIndex");
		this.articulosConsumidos = (ArticulosConsumidos) ((List) this.articulosConsumidosListModel
				.getWrappedData())
				.get(Integer.parseInt(articulosConsumidosDataIndex));
		return AppConstants.ARTICULOS_CONSUMIDOS_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelArticulosConsumidos() {
		return AppConstants.ARTICULOS_CONSUMIDOS_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public ArticulosConsumidos getArticulosConsumidosSearch() {
		return articulosConsumidosSearch;
	}

	/**
	 * 
	 * @param articulosConsumidosSearch
	 */
	public void setarticulosConsumidosSearch(ArticulosConsumidos articulosConsumidosSearch) {
		this.articulosConsumidosSearch = articulosConsumidosSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getarticulosConsumidosListModel() {
		return articulosConsumidosListModel;
	}

	/**
	 * 
	 * @param articulosConsumidosListModel
	 */
	public void setarticulosConsumidosListModel(DataModel articulosConsumidosListModel) {
		this.articulosConsumidosListModel = articulosConsumidosListModel;
	}

	/**
	 * 
	 * @return
	 */
	public ArticulosConsumidos getArticulosConsumidos() {
		return articulosConsumidos;
	}

	/**
	 * 
	 * @param articulosConsumidos
	 */
	public void setarticulosConsumidos(ArticulosConsumidos articulosConsumidos) {
		this.articulosConsumidos = articulosConsumidos;
	}

	/**
	 * obtencion del servicio articulosConsumidosService
	 * 
	 * @return
	 */
	public IArticulosConsumidosService getArticulosConsumidosService() {
		return articulosConsumidosService;
	}

	/**
	 * establecimiento del servicio articulosConsumidosService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param articulosConsumidosService
	 */
	public void setArticulosConsumidosService(
			IArticulosConsumidosService articulosConsumidosService) {
		this.articulosConsumidosService = articulosConsumidosService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
